/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cheatappdatagen;

import assignmentbuilder.AssignmentZipBuilder;
import assignmentbuilder.ContentFile;
import assignmentbuilder.RandomXMLContentFile;
import assignmentbuilder.XMLContentFile;
import cheat.assignment.AssignmentManagerRemote;
import cheat.assignment.InvalidAssignmentArchiveException;
import cheat.authentication.InsufficientAccessException;
import cheat.authentication.InvalidSessionException;
import cheat.authentication.NoSuchRegisteredUserException;
import cheat.authentication.SecurityManagerRemote;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;

/**
 *
 * @author alhshk
 */
public class Main {


  @EJB
  private static SecurityManagerRemote securityMgr;
  @EJB
  private static AssignmentManagerRemote assignmentManagerBean;
  private AssignmentZipBuilder builder;
  final static private String facultyEmail = "safartimuro@gmail.com";
  final static private String facultyPassword = "jbaker";

  public Main() {
    final Set<ContentFile> set1 = new TreeSet<ContentFile>();
    set1.add(new RandomXMLContentFile("assignment.xml"));
    set1.add(new ContentFile("assignment.html"));
    set1.add(new ContentFile("images/screenshot.png"));
    set1.add(new ContentFile("images/interface.png"));
    set1.add(new ContentFile("solution/DiceExperiment.java"));
    set1.add(new ContentFile("solution/1/DiceExperiment.java"));
    set1.add(new ContentFile("solution/2/DiceExperiment.java"));
    set1.add(new ContentFile("software-tests/input1.txt"));
    set1.add(new ContentFile("software-tests/input2.txt"));
    set1.add(new ContentFile("software-tests/public/simple-ex-in.txt"));
    set1.add(new ContentFile("software-tests/public/simple-ex-out.txt"));
    set1.add(new ContentFile("software-tests/public/big-ex-in.txt"));
    set1.add(new ContentFile("software-tests/public/big-ex-out.txt"));
    set1.add(new ContentFile("software-tests/reference/input1.txt"));
    set1.add(new ContentFile("software-tests/reference/expected-out1.txt"));
    set1.add(new ContentFile("software-tests/reference/input2.txt"));
    set1.add(new ContentFile("software-tests/reference/expected-out2.txt"));
    set1.add(new ContentFile("start/.classpath"));
    set1.add(new ContentFile("start/.project"));
    set1.add(new ContentFile("start/build.xml"));
    set1.add(new ContentFile("start/simple-ex-in.txt"));
    set1.add(new ContentFile("start/DiceExperiment.java"));

    builder = new AssignmentZipBuilder(set1);
  }

  private int submitRandom(int facultySessId) throws InvalidSessionException, InsufficientAccessException, InvalidAssignmentArchiveException {
    int uuidSubmit = assignmentManagerBean.submitAssignment( facultySessId,builder.getBytes() );

    System.out.println( "submitAssignment uuid: " + uuidSubmit );

    return  uuidSubmit;
  }

  public void generateAssignments(int facultySessId, int count) throws InvalidSessionException, InsufficientAccessException, InvalidAssignmentArchiveException {

    for ( int idx = 0; idx < count; idx++ ){
      int uuid = submitRandom(facultySessId);
      System.out.println( idx + ": added: " + uuid );
    }
  }

  public static void main(String[] args){
    Main driver = new Main();

    int sessionId = -1;

    System.out.println( "securityMgr = " + (securityMgr==null ?"null" : "not null" ) );
    try {
      try {
        sessionId = securityMgr.login(facultyEmail, facultyPassword);

        int count = 1500;

        driver.generateAssignments(sessionId, count);
      } finally {
        if (sessionId != -1) {
          securityMgr.logout(sessionId);
        }
      }
    } catch (InsufficientAccessException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchRegisteredUserException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidAssignmentArchiveException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidSessionException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}
